//
//  M4PublicNetwork.swift
//  M4
//
//  Created by YuChuang on 2024/8/9.
//

import UIKit

class M4PublicNetwork: NSObject {
    
    static func M4GetAppConfigNet() {
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4setConfig, .post, params: ["KcYkm": "ESP", "YMyhHbqx": M4UserDataManager.m4UserShare.M4Itemcode]), false) { model in
            if model.code == 1 {
                let data = model.data as? [String : Any]
                M4ConfigDataManager.m4ConfigShare.m4servicePhone = data?["HvIKtPfu"] as? String ?? ""
                M4ConfigDataManager.m4ConfigShare.m4adress = data?["RaO"] as? String ?? ""
                M4ConfigDataManager.m4ConfigShare.m4logoUrl = data?["NOtfin"] as? String ?? ""
                M4ConfigDataManager.m4ConfigShare.m4email = data?["XDEwt"] as? String ?? ""
                M4ConfigDataManager.m4ConfigShare.m4appAgreement = data?["CXBX"] as? String ?? ""
                M4ConfigDataManager.m4ConfigShare.m4loanAgreement = data?["YENpgyXXb"] as? String ?? ""
                M4ConfigDataManager.m4ConfigShare.m4companyInfo = data?["ZVewr"] as? String ?? ""
                M4ConfigDataManager.m4ConfigShare.whatsAppStart = data?["HXCzNe"] as? Bool ?? false

                NotificationCenter.default.post(name: Notification.Name("M4ConfigDataManagerm4ConfigShare"), object: nil, userInfo: nil)
            }
        }
    }
    
    static func M4CreditStatusAndInfos(_ needLoading: Bool = true, complete: @escaping (Bool) -> Void) {
        let param: [String : Any] = ["YhBmxwU": M4UserDataManager.m4UserShare.M4Itemcode, "KhlX": "ESP", "EssCCnW": M4UserDataManager.m4UserShare.M4Token, "TlQlI": M4UserDataManager.m4UserShare.M4ProductCode, "KSJb":M4UserDataManager.m4UserShare.M4Version]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4InfoStatus, .post, params: param), needLoading) { model in
            if model.code == 1 {
                let result = model.data as? [String : Any]
                let data = result?["KNUXBVhtgWe"] as? [String : Any]
                M4CreditStatusManager.m4CreditShare.m4LoanCredit = data?["DASDg"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4personalCredit = data?["VhIJV"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4workCredit = data?["NLqs"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4contactCredit = data?["VTnBD"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4identityCredit = data?["CTlUoBX"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4faceCredit = data?["FXFGgsyQ"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4bankCredit = data?["DbYl"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4realName = data?["XemMl"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4mobile = data?["JAdY"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4needRepayAllAmounts = data?["ELmEIN"] as? Double ?? 0
                M4CreditStatusManager.m4CreditShare.m4overdueRepayCount = data?["FUJ"] as? Int ?? 0
                M4CreditStatusManager.m4CreditShare.m4isPassProductDetailPage = result?["YvLHkOucPF"] as? Int ?? 0
                M4CreditStatusManager.m4CreditShare.m4isEmailAuth = result?["kOUsx"] as? Int ?? 0
                M4CreditStatusManager.m4CreditShare.m4isContactCount = result?["tKsdf"] as? Int ?? 2
                M4CreditStatusManager.m4CreditShare.m4phonePattern = result?["uKxq"] as? String ?? ""
                M4CreditStatusManager.m4CreditShare.m4isUserLimitRemark = result?["bKgsf"] as? Int ?? 0
                complete(true)
            }
            else {
                M4BaseViewController.M4CurrentVC()?.view.makeToast(model.message, position: .center)
                complete(false)
            }
        }
    }
    
    static func M4UploadFielNet(imgBase64Str: String, complete: @escaping (Bool, String) -> Void) {
//        let param1: [String : Any] = ["iFeo": imgBase64Str, "WfVD": "fileupload", "hlMoTfbz": M4UserDataManager.m4UserShare.M4Itemcode, "owHpX": "jpg"]
        let param1: [String : Any] = ["ushfx": imgBase64Str, "mjhsdf": "fileupload", "tysdfx": M4UserDataManager.m4UserShare.M4Itemcode, "ohjs": "jpg"]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4uploadFile, .post, params: param1), false) { model in
            if model.code == 1 {
                let data = model.data as? [String : Any]
                let url = data?["tMSDs"] as? String ?? ""
                complete(true, url)
            }
            else {
//                M4BaseViewController.M4CurrentVC()?.view.makeToast(model.message, position: .center)
                complete(false, "")
            }
        }
    }
    
    static func M4CheckNeedUpload(complete: @escaping (Int) -> Void) {
        let loading = M4LoadingAlertView()
        loading.M4LoadingAlertShow()
        let para: [String : Any] = ["ewqoQ": "ESP", "poasJ": M4UserDataManager.m4UserShare.M4Itemcode, "xcaoW": M4UserDataManager.m4UserShare.M4Token, "weqoR": "0"]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4verficationData, .post, params: para), true) { model in
            loading.M4AlertViewHidden()
            if model.code == 1 {
                let data = model.data as? [String : Any]
                let isUpload = data?["weqoQ"] as? Int ?? 1
                complete(isUpload)
            } else {
                complete(1)
            }
        }
    }
    
    static func M4ShowUploadAlert(complete: @escaping (Bool) -> Void) {
        let alert = UINib(nibName: "M4UploadAlertView", bundle: nil).instantiate(withOwner: self).last as? M4UploadAlertView
        alert?.M4AlertViewShow()
        alert?.M4UploadStartAnimation()
        DispatchQueue.main.asyncAfter(deadline: .now()+2) {
            self.M4UploadDataNet { num in
                alert?.M4AnimationRun = false
                if num >= 5 {
                    alert?.M4UploadStopAnimation()
                    complete(true)
                }
                else {
                    alert?.M4UploadChangeUI()
                    complete(false)
                }
            }
        }
    }
    
//    static func M4VerficationPointDataNet(_ needLoading: Bool = true, complete: @escaping (Bool) -> Void) {
//        let para: [String : Any] = ["ewqoQ": "ESP", "poasJ": M4UserDataManager.m4UserShare.M4Itemcode, "xcaoW": M4UserDataManager.m4UserShare.M4Token, "weqoR": "0"]
//        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4verficationData, .post, params: para), needLoading) { model in
//            if model.code == 1 {
//                let data = model.data as? [String : Any]
//                let isUpload = data?["weqoQ"] as? Int ?? 1
//                if isUpload == 1 {
//                    let alert = UINib(nibName: "M4UploadAlertView", bundle: nil).instantiate(withOwner: self).last as? M4UploadAlertView
//                    alert?.M4AlertViewShow()
//                    alert?.M4UploadStartAnimation()
//                    DispatchQueue.main.asyncAfter(deadline: .now()+2) {
//                        self.M4UploadDataNet { num in
//                            alert?.M4AnimationRun = false
//                            if num >= 5 {
//                                alert?.M4UploadStopAnimation()
//                                complete(true)
//                            }
//                            else {
//                                alert?.M4UploadChangeUI()
//                                complete(false)
//                            }
//                        }
//                    }
//                }
//                else {
//                    complete(true)
//                }
//            }
//            else {
//                complete(true)
//            }
//        }
//    }
    
    static func M4UploadDataNet(locationType: String = "3", complete: @escaping (Int) -> Void) {
        var successCount: Int = 0
        M4LocationDataNet(operType: locationType) { num1 in
            successCount += num1
            M4AppPluginsDataNet { num2 in
                successCount += num2
                M4ContactsDataNet { num3 in
                    successCount += num3
                    M4UserDeviceInfo1DataNet { num4 in
                        successCount += num4
                        M4UserDeviceInfo2DataNet { num5 in
                            successCount += num5
                            complete(successCount)
                        }
                    }
                }
            }
        }
    }
    
    static func M4LocationDataNet(operType: String, complete: @escaping (Int) -> Void) {
        let para: [String : Any] = ["Nmy": "ESP", "IVSXc": M4UserDataManager.m4UserShare.M4Itemcode, "KDOwnaIjN": M4UserDataManager.m4UserShare.M4Token, "FDYari": 1, "FjJsPwtI": M4UserDataManager.m4UserShare.M4Longtitude, "RoXpEJ": M4UserDataManager.m4UserShare.M4Latitude, "Avopv": operType]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4pointData, .post, params: para), false) { model in
            if model.code == 1 {
                complete(1)
            }
            else {
                complete(0)
            }
        }
    }
    
    static func M4AppPluginsDataNet(complete: @escaping (Int) -> Void) {
        let plugins = M4String.M4JSONStringFromDict(dictionary: M4AppPlugins())
        let para: [String : Any] = ["Nmy": "ESP", "IVSXc": M4UserDataManager.m4UserShare.M4Itemcode, "KDOwnaIjN": M4UserDataManager.m4UserShare.M4Token, "FDYari": 4, "Qyax": plugins]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4pointData, .post, params: para), false) { model in
            if model.code == 1 {
                complete(1)
            }
            else {
                complete(0)
            }
        }
    }
    
    static func M4ContactsDataNet(complete: @escaping (Int) -> Void) {
        let contacts = M4String.M4JSONStringFromDict(dictionary: M4UserDataManager.m4UserShare.M4ContactData)
        let para: [String : Any] = ["Nmy": "ESP", "IVSXc": M4UserDataManager.m4UserShare.M4Itemcode, "KDOwnaIjN": M4UserDataManager.m4UserShare.M4Token, "FDYari": 5, "AtJTAjohHu": contacts]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4pointData, .post, params: para), false) {model in
//            if model.code == 1 {
//                complete(1)
//            }
//            else {
//                complete(0)
//            }
            complete(1)
        }
    }
    
    static func M4UserDeviceInfo1DataNet(complete: @escaping (Int) -> Void) {
        let para: [String : Any] = ["Nmy": "ESP", "IVSXc": M4UserDataManager.m4UserShare.M4Itemcode, "KDOwnaIjN": M4UserDataManager.m4UserShare.M4Token, "FDYari": 6, "NjYuHpKSY": M4DeviceModel(), "NMAmzPqLMkg": "Apple", "BsXhWbCx": M4UserDataManager.m4UserShare.adjustAdid]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4pointData, .post, params: para), false) { model in
            if model.code == 1 {
                complete(1)
            }
            else {
                complete(0)
            }
        }
    }
    
    static func M4UserDeviceInfo2DataNet(complete: @escaping (Int) -> Void) {
        let para: [String : Any] = ["Nmy": "ESP",
                                    "IVSXc": M4UserDataManager.m4UserShare.M4Itemcode,
                                    "KDOwnaIjN": M4UserDataManager.m4UserShare.M4Token,
                                    "FDYari": 7,
                                    "MQtjG": M4DeviceDataManager.m4DeviceShare.m4MQtjG,
                                    "EPS": M4DeviceDataManager.m4DeviceShare.m4EPS,
                                    "CVQs": M4DeviceDataManager.m4DeviceShare.m4CVQs,
                                    "XwMAiX": M4DeviceDataManager.m4DeviceShare.m4XwMAiX,
                                    "MjgIyPNRpq": M4DeviceDataManager.m4DeviceShare.m4MjgIyPNRpq,
                                    "Sus": M4DeviceDataManager.m4DeviceShare.m4Sus,
                                    "FrnBuE": M4DeviceDataManager.m4DeviceShare.m4FrnBuE,
                                    "ZJDg": M4DeviceDataManager.m4DeviceShare.m4ZJDg,
                                    "BntQavpE": M4DeviceDataManager.m4DeviceShare.m4BntQavpE,
                                    "RAQdnE": M4DeviceDataManager.m4DeviceShare.m4RAQdnE,
                                    "MBhBDco": M4DeviceDataManager.m4DeviceShare.m4MBhBDco,
                                    "PSr": M4DeviceDataManager.m4DeviceShare.m4PSr,
                                    "LRHlRvSA": M4DeviceDataManager.m4DeviceShare.m4LRHlRvSA,
                                    "KFVUV": M4DeviceDataManager.m4DeviceShare.m4KFVUV,
                                    "ECShpPNI": M4DeviceDataManager.m4DeviceShare.m4ECShpPNI,
                                    "CODS": M4DeviceDataManager.m4DeviceShare.m4CODS,
                                    "QhnB": M4DeviceDataManager.m4DeviceShare.m4QhnB,
                                    "SSq": M4DeviceDataManager.m4DeviceShare.m4SSq,
                                    "KEVROxIl": M4DeviceDataManager.m4DeviceShare.m4KEVROxIl - M4DeviceDataManager.m4DeviceShare.m4phoneBeginTime,
                                    "NtQksYpPs": M4DeviceDataManager.m4DeviceShare.m4NtQksYpPs,
                                    "Giczr": M4DeviceDataManager.m4DeviceShare.m4Giczr,
                                    "QtWe": M4DeviceDataManager.m4DeviceShare.m4QtWe,
                                    "TNr": M4DeviceDataManager.m4DeviceShare.m4TNr,
                                    "KDtZZ": M4DeviceDataManager.m4DeviceShare.m4KDtZZ,
                                    "FKlhJeXT": M4DeviceDataManager.m4DeviceShare.m4FKlhJeXT,
                                    "GHPTPehkFK": M4DeviceDataManager.m4DeviceShare.m4GHPTPehkFK,
                                    "FZYOZ": M4DeviceDataManager.m4DeviceShare.m4FZYOZ,
                                    "TZfpuyuyvg": M4DeviceDataManager.m4DeviceShare.m4TZfpuyuyvg,
                                    "Auf": M4DeviceDataManager.m4DeviceShare.m4Auf,
                                    "VZh": M4DeviceDataManager.m4DeviceShare.m4VZh,
                                    "RqI": M4DeviceDataManager.m4DeviceShare.m4RqI,
                                    "PHPwi": M4DeviceDataManager.m4DeviceShare.m4PHPwi,
                                    "GufgdRChqiI": M4DeviceDataManager.m4DeviceShare.m4GufgdRChqiI,
                                    "CqFHWSP": M4DeviceDataManager.m4DeviceShare.m4CqFHWSP,
                                    "Ozas": M4DeviceDataManager.m4DeviceShare.m4Ozas,
                                    "BnnpqGY": M4DeviceDataManager.m4DeviceShare.m4BnnpqGY,
                                    "HMSLenjspX": M4DeviceDataManager.m4DeviceShare.m4HMSLenjspX]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4pointData, .post, params: para), false) { model in
            if model.code == 1 {
                complete(1)
            }
            else {
                complete(0)
            }
        }
    }
    
    static func M4SubmitLoanNet(_ needLoading: Bool = true, paramnet: [String : Any], complete: @escaping (Bool, Int) -> Void) {
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4SubmitLoan, .post, params: paramnet), needLoading) { model in
            if model.code == 1 {
                let data = model.data as? [String : Any]
                let loanid = data?["sadjEPi"] as? Int ?? 0
                complete(true, loanid)
            }
            else {
                M4BaseViewController.M4CurrentVC()?.view.makeToast(model.message, position: .center)
                complete(false, 0)
            }
        }
    }

}
